package com.bbc.action.type;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.bbc.base.BaseAction;
import com.bbc.entity.type.BbcType;
import com.bbc.entity.type.BbcTypeQuery;
import com.bbc.service.type.BbcTypeService;

public class BbcTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private transient BbcTypeService bbcTypeService;
	
	private List<BbcType> typeTypeList;
	private Map<Integer, BbcType> typeTypeMap;
	
	private BbcTypeQuery query;
	private BbcType bbcType;
	private List<BbcType> bbcTypes;
	
	public String list() {
		initTypeType();
		if (query == null) {
			query = new BbcTypeQuery();
		}
		bbcTypes = bbcTypeService.findBbcType(query, page, 20);
		for (BbcType bt:bbcTypes) {
			BbcType parent = typeTypeMap.get(bt.getTypeType());
			if (parent != null) {
				bt.setParentName(parent.getTypeName());
			}
		}
		return "list";
	}
	
	public String initpage() {
		initTypeType();
		return "list";
	}
	
	public String save() {
		bbcType.setTypeType(bbcType.getParentId());
		bbcType.setLevel(2);
		if (bbcType.getTypeId() == null) {
			bbcTypeService.addBbcType(bbcType);
		} else {
			bbcTypeService.updateBbcType(bbcType);
		}
		return list();
	}
	
	private void initTypeType() {
		typeTypeList = bbcTypeService.findBbcType(1);
		typeTypeMap = new HashMap<Integer, BbcType>();
		for (BbcType bt:typeTypeList) {
			typeTypeMap.put(bt.getTypeId(), bt);
		}
	}

	public BbcTypeQuery getQuery() {
		return query;
	}

	public void setQuery(BbcTypeQuery query) {
		this.query = query;
	}

	public BbcType getBbcType() {
		return bbcType;
	}

	public void setBbcType(BbcType bbcType) {
		this.bbcType = bbcType;
	}

	public List<BbcType> getBbcTypes() {
		return bbcTypes;
	}

	public void setBbcTypes(List<BbcType> bbcTypes) {
		this.bbcTypes = bbcTypes;
	}

	public List<BbcType> getTypeTypeList() {
		return typeTypeList;
	}

	public void setTypeTypeList(List<BbcType> typeTypeList) {
		this.typeTypeList = typeTypeList;
	}

	public Map<Integer, BbcType> getTypeTypeMap() {
		return typeTypeMap;
	}

	public void setTypeTypeMap(Map<Integer, BbcType> typeTypeMap) {
		this.typeTypeMap = typeTypeMap;
	}

}
